﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.IO;
using System.Net;
using System.Collections.Specialized;
using System.Security.Cryptography;
using System.Web.Script.Serialization;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Xml;

namespace AliCMS.UserModule.Bll
{
    public partial class User : DbObjectBase, IUser
    {
        public class Money : DbObjectBase, UserModule.User.IMoney
        {
            public Money(int instanceID, string lang, bool checkRight, string database)
                : base(instanceID, lang, checkRight, database)
            {
            }

            public override object ExecuteService(HttpContext context)
            {
                object result = null;

                string operation = context.Request.QueryString["Operation"];
                if (operation == "Add")
                {
                    Dictionary<string, object> item = new Dictionary<string, object>();
                    string value = null;
                    item[Alias.User.Money.UserID] = context.Request.Form["UserID"];
                    value = context.Request.Form["Title"];
                    if (value != null) item[Alias.User.Money.Title] = value;
                    item[Alias.User.Money.Amount] = context.Request.Form["Amount"];
                    value = context.Request.Form["Type"];
                    if (value != null) item[Alias.User.Money.Type] = value;
                    value = context.Request.Form["Time"];
                    if (value != null) item[Alias.User.Money.Time] = value;
                    value = context.Request.Form["Frozen"];
                    if (value != null) item[Alias.User.Money.Frozen] = value;
                    value = context.Request.Form["Memo"];
                    if (value != null) item[Alias.User.Money.Memo] = value;
                    value = context.Request.Form["CreatorID"];
                    if (value != null) item[Alias.User.Money.CreatorID] = value;
                    value = context.Request.Form["CreatorName"];
                    if (value != null) item[Alias.User.Money.CreatorName] = value;
                    value = context.Request.Form["CreateTime"];
                    if (value != null) item[Alias.User.Money.CreateTime] = value;

                    result = Add(item);
                }
                else if (operation == "Modify")
                {
                    int id = Convert.ToInt32(context.Request.QueryString["ID"]);

                    Dictionary<string, object> changes = new Dictionary<string, object>();
                    string value = null;
                    value = context.Request.Form["Title"];
                    if (value != null) changes[Alias.User.Money.Title] = value;
                    value = context.Request.Form["Time"];
                    if (value != null) changes[Alias.User.Money.Time] = value;
                    value = context.Request.Form["Frozen"];
                    if (value != null) changes[Alias.User.Money.Frozen] = value;
                    value = context.Request.Form["Memo"];
                    if (value != null) changes[Alias.User.Money.Memo] = value;

                    Modify(id, changes);
                }
                else if (operation == "Delete")
                {
                    string id = context.Request.QueryString["ID"];

                    string[] ids = id.Split('|');
                    for (int i = 0; i < ids.Length; i++)
                    {
                        if (String.IsNullOrEmpty(ids[i]))
                            continue;

                        Delete(Convert.ToInt32(ids[i]));
                    }
                }
                else if (operation == "List")
                {
                    string value = null;

                    int page = -1;
                    value = context.Request.QueryString["Page"];
                    if (value != null) page = Convert.ToInt32(value);

                    int pageSize = -1;
                    value = context.Request.QueryString["PageSize"];
                    if (value != null) pageSize = Convert.ToInt32(value);

                    string condition = context.Request.QueryString["Condition"];
                    string sort = context.Request.QueryString["Sort"];

                    result = List(page, pageSize, condition, sort);
                }
				else if (operation == "GetBalance")
				{
					Guid userId = new Guid(context.Request.QueryString["UserID"]);
					result = GetBalance(userId);
				}
                else if (operation == "GetIncome")
                {
                    Guid userId = new Guid(context.Request.QueryString["UserID"]);
                    int days = Convert.ToInt32(context.Request.QueryString["Days"]);
                    result = GetIncome(userId, days);
                }
                else if (operation == "User_StatSumOne")
                {
                    Dictionary<string, object> data = new Dictionary<string, object>();
                    string value = null;
                    value = context.Request.QueryString["Start"];
                    if (value != null) data["Start"] = value;
                    value = context.Request.QueryString["End"];
                    if (value != null) data["End"] = value;
                    value = context.Request.QueryString["Income"];
                    if (value != null) data["Income"] = value;
                    value = context.Request.QueryString["Type"];
                    if (value != null) data["Type"] = value;
                    value = context.Request.QueryString["Frozen"];
                    if (value != null) data["Frozen"] = value;

                    result = User_StatSumOne(new Guid(context.Request.QueryString["UserID"]), data);
                }
                else if (operation == "User_StatSumAll")
                {
                    Dictionary<string, object> data = new Dictionary<string, object>();
                    string value = null;
                    value = context.Request.QueryString["Start"];
                    if (value != null) data["Start"] = value;
                    value = context.Request.QueryString["End"];
                    if (value != null) data["End"] = value;
                    value = context.Request.QueryString["Income"];
                    if (value != null) data["Income"] = value;
                    value = context.Request.QueryString["Type"];
                    if (value != null) data["Type"] = value;
                    value = context.Request.QueryString["Frozen"];
                    if (value != null) data["Frozen"] = value;

                    result = User_StatSumAll(data);
                }
                else if (operation == "User_StatSumHourly")
                {
                    Dictionary<string, object> data = new Dictionary<string, object>();
                    string value = null;
                    value = context.Request.QueryString["Start"];
                    if (value != null) data["Start"] = value;
                    value = context.Request.QueryString["End"];
                    if (value != null) data["End"] = value;
                    value = context.Request.QueryString["Income"];
                    if (value != null) data["Income"] = value;
                    value = context.Request.QueryString["Type"];
                    if (value != null) data["Type"] = value;
                    value = context.Request.QueryString["Frozen"];
                    if (value != null) data["Frozen"] = value;

                    result = User_StatSumHourly(new Guid(context.Request.QueryString["UserID"]), data);
                }
                else if (operation == "User_StatSumDaily")
                {
                    Dictionary<string, object> data = new Dictionary<string, object>();
                    string value = null;
                    value = context.Request.QueryString["Start"];
                    if (value != null) data["Start"] = value;
                    value = context.Request.QueryString["End"];
                    if (value != null) data["End"] = value;
                    value = context.Request.QueryString["Income"];
                    if (value != null) data["Income"] = value;
                    value = context.Request.QueryString["Type"];
                    if (value != null) data["Type"] = value;
                    value = context.Request.QueryString["Frozen"];
                    if (value != null) data["Frozen"] = value;

                    result = User_StatSumDaily(new Guid(context.Request.QueryString["UserID"]), data);
                }
                else if (operation == "User_StatSumMonthly")
                {
                    Dictionary<string, object> data = new Dictionary<string, object>();
                    string value = null;
                    value = context.Request.QueryString["Start"];
                    if (value != null) data["Start"] = value;
                    value = context.Request.QueryString["End"];
                    if (value != null) data["End"] = value;
                    value = context.Request.QueryString["Income"];
                    if (value != null) data["Income"] = value;
                    value = context.Request.QueryString["Type"];
                    if (value != null) data["Type"] = value;
                    value = context.Request.QueryString["Frozen"];
                    if (value != null) data["Frozen"] = value;

                    result = User_StatSumMonthly(new Guid(context.Request.QueryString["UserID"]), data);
                }
                else
                {
                    throw new Exception("lang_header_param");
                }

                return result;
            }

            public int GetInnerIDFromGuid(Guid guid)
            {
                return Dal.GetInnerIDFromGuid(guid);
            }
            public Guid GetGuidFromInnerID(object id)
            {
                return Dal.GetGuidFromInnerID(id);
            }
            public Dictionary<string, object> Get(object id)
            {
                if (CheckRight)
                {
                    Guid guid;
                    if (id is Guid)
                        guid = (Guid)id;
                    else
                        guid = Dal.GetGuidFromInnerID(id);
                    ValidateRight("Get", guid);
                }

                return Dal.Get(id);
            }
            public Guid Add(Dictionary<string, object> item)
            {
                if (CheckRight)
                {
                    ValidateRight("Add", Guid.Empty);
                }

                if (!item.ContainsKey(Alias.User.Money.ID) || item[Alias.User.Money.ID].ToString() == String.Empty)
                    item[Alias.User.Money.ID] = Guid.NewGuid();
                else if (!(item[Alias.User.Money.ID] is Guid))
                    item[Alias.User.Money.ID] = new Guid(item[Alias.User.Money.ID].ToString());

                if (!item.ContainsKey(Alias.User.Money.No) || String.IsNullOrEmpty(Convert.ToString(item[Alias.User.Money.No])))
                    item[Alias.User.Money.No] = DateTime.Now.ToString("yyyyMMddHHmmssfff");

                item[Alias.User.Money.UserID] = new Guid(item[Alias.User.Money.UserID].ToString());

                if (item.ContainsKey(Alias.User.Money.Memo))
                {
                    if (String.IsNullOrEmpty(Convert.ToString(item[Alias.User.Money.Memo])))
                        item[Alias.User.Money.Memo] = DBNull.Value;
                }

                if (item.ContainsKey(Alias.User.Money.Time) && !(item[Alias.User.Money.Time] is DateTime))
                {
                    if (String.IsNullOrEmpty(Convert.ToString(item[Alias.User.Money.Time])))
                        item[Alias.User.Money.CreateTime] = DateTime.Now;
                    else
                        item[Alias.User.Money.Time] = Convert.ToDateTime(item[Alias.User.Money.Time]);
                }

                if (item.ContainsKey(Alias.User.Money.OrderID))
                {
                    string orderId = item[Alias.User.Money.OrderID].ToString();
                    if (orderId == String.Empty || orderId == Guid.Empty.ToString())
                        item[Alias.User.Money.OrderID] = DBNull.Value;
                    else
                        item[Alias.User.Money.OrderID] = new Guid(orderId);
                }
                if (item.ContainsKey(Alias.User.Money.OrderInstanceID))
                {
                    string orderInstanceId = item[Alias.User.Money.OrderInstanceID].ToString();
                    if (orderInstanceId == String.Empty || orderInstanceId == Guid.Empty.ToString())
                        item[Alias.User.Money.OrderInstanceID] = DBNull.Value;
                    else
                        item[Alias.User.Money.OrderInstanceID] = new Guid(orderInstanceId);
                }

                if (item.ContainsKey(Alias.User.Money.CreatorID) && !String.IsNullOrEmpty(Convert.ToString(item[Alias.User.Money.CreatorID])))
                {
                    item[Alias.User.Money.CreatorID] = new Guid(item[Alias.User.Money.CreatorID].ToString());
                }
                else
                {
                    Dictionary<string, string> user = Manager.User;
                    if (user != null)
                    {
                        item[Alias.User.Money.CreatorName] = user["Name"];
                        item[Alias.User.Money.CreatorID] = new Guid(user["ID"]);
                    }
                }

                Dal.Add(item);

                return (Guid)item[Alias.User.Money.ID];
            }
            public void Modify(int id, Dictionary<string, object> changes)
            {
                if (CheckRight)
                {
                    Guid guid = Dal.GetGuidFromInnerID(id);
                    ValidateRight("Modify", guid);
                }

                if (changes.ContainsKey(Alias.User.Money.Memo))
                {
                    if (String.IsNullOrEmpty(Convert.ToString(changes[Alias.User.Money.Memo])))
                        changes[Alias.User.Money.Memo] = DBNull.Value;
                }

                if (changes.ContainsKey(Alias.User.Money.Time) && !(changes[Alias.User.Money.Time] is DateTime))
                {
                    string time = changes[Alias.User.Money.Time].ToString();
                    if (!String.IsNullOrEmpty(time))
                        changes[Alias.User.Money.Time] = Convert.ToDateTime(time);
                }

                if (changes.ContainsKey(Alias.User.Money.OrderID))
                {
                    string orderId = changes[Alias.User.Money.OrderID].ToString();
                    if (orderId == String.Empty || orderId == Guid.Empty.ToString())
                        changes[Alias.User.Money.OrderID] = DBNull.Value;
                    else
                        changes[Alias.User.Money.OrderID] = new Guid(orderId);
                }
                if (changes.ContainsKey(Alias.User.Money.OrderInstanceID))
                {
                    string orderInstanceId = changes[Alias.User.Money.OrderInstanceID].ToString();
                    if (orderInstanceId == String.Empty || orderInstanceId == Guid.Empty.ToString())
                        changes[Alias.User.Money.OrderInstanceID] = DBNull.Value;
                    else
                        changes[Alias.User.Money.OrderInstanceID] = new Guid(orderInstanceId);
                }

                Dal.Modify(id, changes);
            }
            public void Delete(int id)
            {
                if (CheckRight)
                {
                    Guid guid = Dal.GetGuidFromInnerID(id);
                    ValidateRight("Delete", guid);
                }

                Dal.Delete(id);
            }
            public DataTable List(int page, int pageSize, string condition, string sort)
            {
                if (CheckRight)
                {
                    ValidateRight("List", Guid.Empty);
                }

                return Dal.List(page, pageSize, condition, sort);
            }
            public int GetRecordCount(string condition)
            {
                return Dal.GetRecordCount(condition);
            }

            public decimal GetBalance(Guid userId)
            {
                if (CheckRight)
                {
                    ValidateRight("GetBalance", Guid.Empty);
                }

                return Dal.GetBalance(userId);
            }
            public decimal GetAllBalance(Guid userId)
            {
                if (CheckRight)
                {
                    ValidateRight("GetAllBalance", Guid.Empty);
                }

				return Dal.GetAllBalance(userId);
            }
            public decimal GetDrawableBalance(Guid userId)
            {
                if (CheckRight)
                {
                    ValidateRight("GetDrawableBalance", Guid.Empty);
                }

				return Dal.GetDrawableBalance(userId);
            }
            public decimal GetIncome(Guid userId, int days)
            {
                if (CheckRight)
                {
                    ValidateRight("GetIncome", Guid.Empty);
                }

                return Dal.GetIncome(userId, days);
            }


            #region 用户统计
            public string User_StatSumOne(Guid userId, Dictionary<string, object> data)
            {
                if (data.ContainsKey("Start"))
                {
                    data["Start"] = Convert.ToDateTime(data["Start"]);
                }

                if (data.ContainsKey("End"))
                {
                    data["End"] = Convert.ToDateTime(data["End"]);
                }

                return Dal.User_StatSumOne(userId, data);
            }
            public string User_StatSumAll(Dictionary<string, object> data)
            {
                if (data.ContainsKey("Start"))
                {
                    data["Start"] = Convert.ToDateTime(data["Start"]);
                }

                if (data.ContainsKey("End"))
                {
                    data["End"] = Convert.ToDateTime(data["End"]);
                }

                return Dal.User_StatSumAll(data);
            }
            public string User_StatSumHourly(Guid userId, Dictionary<string, object> data)
            {
                if (data.ContainsKey("Start"))
                {
                    data["Start"] = Convert.ToDateTime(data["Start"]);
                }

                if (data.ContainsKey("End"))
                {
                    data["End"] = Convert.ToDateTime(data["End"]);
                }

                return Dal.User_StatSumHourly(userId, data);
            }
            public string User_StatSumDaily(Guid userId, Dictionary<string, object> data)
            {
                if (data.ContainsKey("Start"))
                {
                    data["Start"] = Convert.ToDateTime(data["Start"]);
                }

                if (data.ContainsKey("End"))
                {
                    data["End"] = Convert.ToDateTime(data["End"]);
                }

                return Dal.User_StatSumDaily(userId, data);
            }
            public string User_StatSumMonthly(Guid userId, Dictionary<string, object> data)
            {
                if (data.ContainsKey("Start"))
                {
                    data["Start"] = Convert.ToDateTime(data["Start"]);
                }

                if (data.ContainsKey("End"))
                {
                    data["End"] = Convert.ToDateTime(data["End"]);
                }

                return Dal.User_StatSumMonthly(userId, data);
            }
            #endregion

            #region Properties
            private Dal.User.IMoney Dal
            {
                get { return (Dal.User.IMoney)DalBase; }
            }
            #endregion
        }
    }
}
